package com.zhugang.week13.backtrack;

import java.util.ArrayList;
import java.util.List;

/**
 * @program algorithms
 * @description: permute
 * @author: chanzhugang
 * @create: 2022/09/13 01:52
 */
public class Permute {

    /**
     * 46 全排列
     * https://leetcode.cn/problems/permutations/
     *
     * @param nums
     * @return
     */

    List<List<Integer>> result = new ArrayList<>();

    public List<List<Integer>> permute(int[] nums) {
        // 穷举问题，套回溯模版
        List<Integer> path = new ArrayList<>();
        backtrack(nums, 0, path);
        return result;
    }

    private void backtrack(int[] nums, int k, List<Integer> path) {

        if (k == nums.length) {
            // 得到可行解
            result.add(new ArrayList<>(path));
        }

        for (int i = 0; i < nums.length; i++) {
            if (path.contains(nums[i])) {
                // 可选列表：去除path已经决策的数据
                continue;
            }
            path.add(nums[i]);

            // 决策下一阶段
            backtrack(nums, i + 1, path);
            path.remove(path.size() - 1);

        }
    }
}